package com.cn.gmall.list.controller;


import com.cn.gmall.common.result.Result;
import com.cn.gmall.list.service.SearchService;

import com.cn.gmall.model.list.Goods;


import com.cn.gmall.model.list.SearchParam;
import com.cn.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("/api/list")
public class ListApiController {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Resource
    private SearchService searchService;

    /**
     *
     * 商品搜索
     * /api/list
     * @param searchParam
     * @return
     */

    @PostMapping()
    public Result list(@RequestBody SearchParam searchParam) {
//        顶部查询框搜索
        SearchResponseVo responseVo = searchService.topSearchList(searchParam);
        return Result.ok(responseVo);
    }

    /**
     * 更新商品的热度排名
     * /api/list/inner/incrHotScore/{skuId}
     *
     * @param skuId
     * @return
     */
    @GetMapping("/inner/incrHotScore/{skuId}")
    public Result incrHotScore(@PathVariable Long skuId) {
        searchService.incrHotScore(skuId);
        return Result.ok();
    }

    /**
     * 商品下架
     * /api/list/inner/lowerGoods/{skuId}
     *
     * @param skuId
     * @return
     */
    @GetMapping("/inner/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable Long skuId) {
        searchService.lowerGoods(skuId);
        return Result.ok();
    }


    /***
     * /api/list/inner/upperGoods/{skuId}
     * 商品上架
     * @param skuId
     * @return
     */
    @GetMapping("/inner/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable Long skuId) {
        searchService.upperGoods(skuId);
        return Result.ok();
    }


    /**
     * 创建elasticsearch数据库和 索引表
     */
    @GetMapping("/inner/createIndex")
    public Result createIndex() {
        elasticsearchRestTemplate.createIndex(Goods.class);
        elasticsearchRestTemplate.putMapping(Goods.class);

        return Result.ok();
    }

}
